﻿#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);

    init();
}

Widget::~Widget()
{
    delete ui;
}

/*
*/

//开始
void Widget::on_btnStart_clicked()
{
    //判断轮询时间
    QString str=ui->leTime->text();
    if(str.isEmpty()){
        QMessageBox::information(this,"提示信息","请输入轮询时间","确定");
        return;
    }
    int s=str.toUInt();
    if(s<=100||s>1000){
        QMessageBox::information(this,"提示信息","轮询时间输入范围在100`1000","确定");
        return;
    }
    //判断X轴数据点数
    QString ds=ui->leXds->text();
    if(ds.isEmpty()){
        QMessageBox::information(this,"提示信息","请输入X轴数据点数","确定");
        return;
    }
    int ids=ds.toUInt();
    if(ids<=50||ids>200){
        QMessageBox::information(this,"提示信息","X轴数据点数输入范围在50`200","确定");
        return;
    }
    int lxtime=s;
    if(!timer->isActive()){
        timer->start(lxtime);
    }else{
        timer->stop();
        timer->start(lxtime);
    }
    ui->btnStart->setEnabled(false);
    ui->btnStop->setEnabled(true);
}

//暂停
void Widget::on_btnStop_clicked()
{
    timer->stop();
    //设置暂停后可以拖动
    custom->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom | QCP::iSelectPlottables);
    ui->btnStart->setEnabled(true);
    ui->btnStop->setEnabled(false);
}

//清空
void Widget::on_b_clicked()
{
    int ds=ui->leXds->text().toUInt();
    for (int i=0;i<ds;i++) {
        arrX.append(i);
        arrYa.append(0);
        arrYb.append(0);
    }
}

//初始化
void Widget::init()
{
    //设置智能输入数字
    ui->leTime->setValidator(new QRegExpValidator(QRegExp("[0-9$]")));
    ui->leXds->setValidator(new QRegExpValidator(QRegExp("[0-9$]")));
    this->setWindowFlags(Qt::FramelessWindowHint);

    //设置默认值
    ui->leTime->setText("200");
    ui->leXds->setText("100");
    //初始化XY Y2
    int ds=ui->leXds->text().toUInt();
    for (int i=0;i<ds;i++) {
        arrX.append(i);
        arrYa.append(0);
        arrYb.append(0);
    }

    timer=new QTimer(this);
    connect(timer,&QTimer::timeout,this,&Widget::currentMain);

    custom=ui->wCustom;
    custom->legend->setVisible(true);//显示图例
    custom->addGraph();//添加第一条曲线
    custom->graph(0)->setPen(QPen(Qt::blue));
    custom->graph(0)->setBrush(QBrush(QColor(32,178,170)));
    custom->graph(0)->setName("第一个示例");

    custom->addGraph();//添加第二条曲线
    custom->graph(1)->setPen(QPen(Qt::green));
    custom->graph(1)->setBrush((QBrush(QColor(0,0,255,20))));
    custom->graph(1)->setName("第二个示例");

    custom->xAxis->setLabel("X轴");
    custom->yAxis->setLabel("Y轴");
}

void Widget::currentMain()
{
    QString ua5=QString::number(arrX.count());
    ui->label_5->setText(ua5);
    int ds=ui->leXds->text().toUInt();
    if(arrYa.count()>ds){
        arrYa.removeFirst();
        arrYb.removeFirst();
    }
    double ynum=qrand()%125;
    arrYa.append(ynum);
    arrYb.append(0-ynum);
    custom->graph(0)->setData(arrX,arrYa);//写入数据
    custom->graph(1)->setData(arrX,arrYb);
    custom->rescaleAxes();//范围自动调整
    custom->replot();
}

//保存图片
void Widget::on_pushButton_clicked()
{
    QString filename=QFileDialog::getSaveFileName();
    custom->savePng(filename,custom->width(),custom->height());
}

void Widget::on_pushButton_2_clicked()
{
    app->exit(0);
}
